package so.shanku.essential.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.text.TextUtils;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import net.tsz.afinal.json.JsonMap;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.view.CircleImageView;

/**
 * 保存用户个人信息
 */
public class Utils {

    public static final String SP_NAME = "essential";
	public static final String SP_USERNAME = "userName";
	public static final String SP_USER_NICKNAME = "userNickName";
	public static final String SP_HEADICON = "headicon";
	public static final String SP_KEY_USERID = "userId";
	/**
	 * 用户描述
	 */
	public static final String SP_USER_DISCRI ="user_discri";
	/**
	 * 用户手机号
	 */
	public static final String SP_UserPhone = "userPhone";
    /**
     * 用户类型
     */
    public static final String SP_USERTYPE="userType";
    /**
     * 用户邮箱
     */
    public static final String SP_UserEmail = "userEmail";
    /**
     * 用户真实姓名
     */
    public static final String SP_USERREALNAME = "userRealName";
    /**
     * 用户性别（0为男，1为女，2为保密）
     */
    public static final String SP_USERSEX = "userSex";
    /**
     * 用户图片
     */
    public static final String Sp_USER_ICON = "userIcon";
    /**
     * 用户注册时间
     */
    public static final String Sp_UserRegisterTime = "userRegisterTime";
    /**
     * 访客的Account
     */
    public static final String SP_Visiter_Account = "visiteraccount";
    /**
     * 用来判断用户是访客登录还是注册登录
     */
    public static final String SP_UserLoginStyle= "isvisiter";

	/**
	 * 是否已登录
	 * @param context
	 * @return
	 */
	public  static boolean isHasLogin(Context context){
		String userId=getUserId(context);
        return !TextUtils.isEmpty(userId);
	}
    /**
     * 获取用户账户
     */
    public static String getUserAccount(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getString(SP_USERNAME, "");
    }


    /**
     * 获取用户注册时间
     */
    public static String getRegisterTime(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getString(Sp_UserRegisterTime, "");
    }

    /**
     * 获取用户昵称
     *
     * @param context
     * @return
     */
    public static String getUserNickNameOrAccount(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        if(TextUtils.isEmpty(sp.getString(SP_USER_NICKNAME, ""))){
            return  sp.getString(SP_USERNAME, "");
        }else{
            return sp.getString(SP_USER_NICKNAME, "");
        }


    }

    /**
     * 获取用户昵称
     *
     * @param context
     * @return
     */
    public static String getUserNickName(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
         return sp.getString(SP_USER_NICKNAME, "");

    }

    /**
     * 获取用户描述
     * @param context
     * @return
     */
    public static String getUserDiscri(Context context){
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getString(SP_USER_DISCRI, "");
    }

    public static void setUserDiscri(Context context,String userDiscri ){
        SharedPreferences preferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(SP_USER_DISCRI, userDiscri);
        editor.commit();
    }
    /**
     * 获取用户手机号码
     */
    public static String getUserPhone(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getString(SP_UserPhone, "");
    }

    /**
     * 获取用户邮箱
     */
    public static String getUserEmail(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getString(SP_UserEmail, "");
    }

    /**
     * 获取用户性别
     */
    public static int getUserSex(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getInt(SP_USERSEX, 2);
    }


    /**
     * 保存用户图片路径
     */
    public static void saveUserPic(Context context, String picUrl) {
        SharedPreferences preferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(Sp_USER_ICON, picUrl);
        editor.commit();
    }

    /**
     * 保存用户邮箱
     */
    public static void saveUserEmail(Context context, String email) {
        SharedPreferences preferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(SP_UserEmail, email);
        editor.commit();
    }

    /**
     * 保存用户昵称
     */
    public static void saveUserNickName(Context context, String nickName) {
        SharedPreferences preferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(SP_USER_NICKNAME, nickName);
        editor.commit();
    }

    /**
     * 保存用户性别
     */
    public static void saveUserSex(Context context, int sex) {
        SharedPreferences preferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(SP_USERSEX, sex);
        editor.commit();
    }
    /**
     * 保存是访客登录还是注册登录
     */
    public static void saveUserLoginStyle(Context context, boolean isvisiter) {
        SharedPreferences preferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(SP_UserLoginStyle, isvisiter);
        editor.commit();
    }
    /**
     * 获取是访客登录还是注册登录
     */
    public static boolean getUserLoginStyle(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getBoolean(SP_UserLoginStyle,false);
    }
    /**
     * 保存用户真实姓名
     */
    public static void saveUserRealName(Context context, String realName) {
        SharedPreferences preferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(SP_USERREALNAME, realName);
        editor.commit();
    }
    /**
     * 保存访客的TempUserAccount
     */
    public static void saveVisiter(Context context,String realName) {
        SharedPreferences preferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(SP_Visiter_Account, realName);
        editor.commit();
    }
    /**
     * 获取访客的TempUserAccount
     */
    public static String getVisiterAccount(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getString(SP_Visiter_Account,"");
    }
    /**
     * 获取用户id
     *
     * @param context
     * @return
     */
    public static String getUserId(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
       	return sp.getString(SP_KEY_USERID, "10841");
    }

    /**
     * 获取用户
     */
    public static String getUserRealName(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getString(SP_USERREALNAME, "");
    }

    /**
     * 获取用户图片
     */
    public static String getUserPic(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        String userName = preferences.getString(Sp_USER_ICON, "");
        return userName;
    }


	/**
	 * 登录注册保存用户信息
	 */
	public static void saveUserInfos(Context context, JsonMap<String, Object> data) {
		SharedPreferences preferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		/**
		 * 用户ID
		 */
		editor.putString(SP_KEY_USERID, data.getStringNoNull("userId"));
		/**
		 * 用户手机号码
		 */
		editor.putString(SP_UserPhone, data.getStringNoNull("userPhone"));
		/**
		 * 用户昵称
		 */
		editor.putString(SP_USER_NICKNAME, data.getStringNoNull("nickName"));
		/**
		 * 用户邮箱
		 */
		editor.putString(SP_UserEmail, data.getStringNoNull("email"));

		/**
		 * 用户性别
		 */
		editor.putInt(SP_USERSEX, data.getInt("sex", 2));
		/**
		 * 用户图片
		 */
		editor.putString(Sp_USER_ICON,data.getStringNoNull("userIcon"));

        /**
         * 用户名称
         */
        editor.putString(SP_USERNAME,data.getStringNoNull("userName"));
        editor.putString(SP_USER_DISCRI,data.getStringNoNull("userDescri"));
        editor.putInt(SP_USERTYPE,data.getInt("userType"));
		editor.commit();
	}

	/**
	 * 	退出登录 要保存
	 */
	public static void clearUserInfoData(Context context){
		SharedPreferences preferences=context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		String userPhone=preferences.getString(SP_UserPhone, "");
		SharedPreferences.Editor editor=preferences.edit();
		editor.clear();
		editor.putString(SP_UserPhone,userPhone);
		editor.commit();
	}

    /**
     * 保存手机号
     * @param mContext
     * @param phone
     */
    public static void saveUserPhone(Context mContext, String phone) {
        SharedPreferences preferences=mContext.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();
        editor.putString(SP_UserPhone,phone);
        editor.commit();
    }

	/**
	 * textview 加下划线
	 * @param textView
	 */
	public static void addUnderLine(TextView textView) {
        textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG ); //下划线
		textView.getPaint().setAntiAlias(true);//抗锯齿
	}

	/**
	 * textview 加删除线
	 * @param textView
	 */
	public static void addDeleteLine(TextView textView) {
		textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //下划线
		textView.getPaint().setAntiAlias(true);//抗锯齿
	}

    /**
     * 擦除删除线
     * @param textView
     */
    public static void crashDeleteLine(TextView textView){
        textView.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG); //去除下划线
        textView.getPaint().setAntiAlias(true);//抗锯齿
    }

    private static long lastBrandClickTime;
    public static boolean isBrandFastClick() {
        long time = System.currentTimeMillis();
        if ( time - lastBrandClickTime < 200) {
            return true;
        }
        lastBrandClickTime = time;
        return false;
    }

    private static long lastClickTime;
    public static boolean isFastClick() {
        long time = System.currentTimeMillis();
        if ( time - lastClickTime < 800) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    /**
     * 用户头像设置路径,和边框颜色
     * @param jsonMap
     * @param user_photo_iv
     */
    public static void setUserIcon(JsonMap jsonMap, CircleImageView user_photo_iv) {
        ImageLoader.getInstance().displayImage(jsonMap.getStringNoNull("userIcon"),user_photo_iv, MyApplication.getInstance().getUserHeadOption());
        // user_photo_iv.setBorderColor();

    }
}
